<template>
	<view class="content">
		<view class="top-bar">
			<view class="search">
				<input type="text" placeholder="请输入" @input="search" />
				<image src="../../static/img/search/search.png" mode=""></image>
			</view>
			<view class="top-bar-right" @tap="backOne">
				取消
			</view>
		</view>
		<view class="main">
			<view class="user">
				<view class="title">
					用户
				</view>
				<view class="list" v-for="item in userList" :key="item.id">
					<view class="avatar" @click="ToUserhome(item.id)">
						<image :src="item.avatar" mode=""></image>
					</view>
					<view class="names">
						<view class="name" v-html="item.name">
							<!-- {{item.name}} -->
						</view>
						<view v-html="item.email">
							
						</view>
					</view>
					<view class="button added" v-if="item.isfriend" >
						发消息
					</view>
					<view class="button add" v-else @tap="showdialog(item.id)">
						加好友
					</view>
				</view>
			</view>
			<view class="group">
				<view class="title">
					群组
				</view>
				<view class="list" v-for="( item,i) in groups" :key="item.id">
					<view class="avatar" @tap="topage(item.id)">
						<image :src="item.cover" mode=""></image>
					</view>
					<view class="names">
						<view class="name">
							{{item.name}}
						</view>
						<!-- <view>
							23562621
						</view> -->
					</view>
					<view class="button added" v-if="item.isgroup" >
						发消息
					</view>
					<view class="button add" v-else @tap="addgroup(item.id,i)">
						加群
					</view>
				</view>
			</view>
		</view>
		<uni-popup ref="inputDialog" type="dialog">
			<uni-popup-dialog ref="inputClose" mode="input" title="添加好友" v-model="text" placeholder="请输入验证消息"
				@confirm="dialogInputConfirm"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import data from '../../common/js/datas.js'
	import request from '../../common/js/request.js'
	import {mapState} from 'vuex'
	export default {
		data() {
			return {
				userList: [],
				text:'',
				fid:'',
				groups:[]
			}
		},
		methods: {
			search(e) {
				this.userList = []
				var key = e.detail.value
				if(key.length != 0 ){
					 this.searchUser(key)
					 this.searchgroup(key)
				}
				 
				
			},
			searchUser(key) {
				if(timer){
					clearTimeout(timer)
				}
				var timer = setTimeout(async ()=>{
					var { data } = await request({
						url:'/search/user',
						data:{
							key,
							id:this.id
						}
					})
					if(data.status ==200){
						this.userList =data.data
						// console.log(this.userList);
						this.userList.forEach(item=>{
							if(item.name.indexOf(key)!==-1){
								var arr = item.name.split(key)
								item.name=`${arr[0]}<text style="color:#4AAAFF">${key}</text>${arr[1]}`
							}else{
								var arr = item.email.split(key)
								item.email= `${arr[0]}<text style="color:blue">${key}</text>${arr[1]}`
							}
						})
					}
				},1000)			
			},
			searchgroup(key){
				if(timer){
					clearTimeout(timer)
				}
				var timer = setTimeout(async ()=>{
					var { data } = await request({
						url:'/search/group',
						data:{
							key,
							id:this.id
						}
					})
					if(data.status ==200){
						this.groups = data.data
						this.userList.forEach(item=>{
							if(item.name.indexOf(key)!==-1){
								var arr = item.name.split(key)
								item.name=`${arr[0]}<text style="color:#4AAAFF">${key}</text>${arr[1]}`
							}
						})
					}
				},1000)
			},
			backOne() {
				uni.navigateBack()
			},
			ToUserhome(id) {
				uni.navigateTo({
					url: '/pages/userhome/userhome?id=' + id
				})
			},
			showdialog(e){
				this.text = this.username+',请求添加好友~'
				this.$refs.inputDialog.open()
				this.fid = e
			},
			async dialogInputConfirm(){
				var {data}= await request({
					url:'/friend/add',
					method:"POST",
					data:{
						uid:this.id,
						fid:this.fid,
						msg:this.text
					}
				})
				if(data.status == 200){
					uni.showToast({
						title:"好友请求已发送"
					})
				}
				this.fid = ''
				this.text =''
				this.$refs.inputDialog.close()
			},
			async addgroup(id,i){
				var {data} = await this.request({
					url:'/group/add',
					method:'POST',
					data:{
						userlist:[{id:this.id}],
						gid:id
					}
				})
				this.groups[i].isgroup = true
			},
			topage(id){
				uni.navigateTo({
					url:'/pages/groupdetail/groupdetail?id='+id
				})
			}
		},
		
		computed:{
			...mapState(['username','id','FriendList','group'])
		},
	}
</script>

<style lang="scss" scoped>
	@import "../../common/css/base.scss";

	.top-bar {
		@include top-bar;

		.search {
			float: left;
			padding: 14rpx 0;
			margin-right: 30rpx;
			position: relative;

			input {
				height: 60rpx;
				width: 600rpx;
				background: #F3F4F6;
				border-radius: 10rpx;
				line-height: 60rpx;
				padding-left: 12rpx;
				padding-right: 52rpx;
				box-sizing: border-box;
			}

			image {
				position: absolute;
				width: 40rpx;
				height: 40rpx;
				top: 24rpx;
				right: 12rpx;
			}
		}

		.top-bar-right {
			font-size: 28rpx;
			color: #272832;
			letter-spacing: -0.48rpx;
			font-weight: 400;
			line-height: 48rpx;
		}
	}

	// .content {
	// 	padding-top: var(--status-bar-height);
	// 	padding-bottom: 18rpx;
	// }

	.main {
		padding: 88rpx 32rpx 0;

		.user,
		.group {
			padding-top: 40rpx;
			min-height: 80rpx;

			.title {
				font-size: 44rpx;
				color: #272832;
				letter-spacing: -0.75rpx;
				font-weight: 600;
			}

			.list {
				padding: 20rpx 0;
				display: flex;
				align-items: center;

				.avatar {
					image {
						width: 80rpx;
						height: 80rpx;
						border-radius: 20rpx;
						background-color: #FFE431
					}
				}

				.names {
					padding-left: 32rpx;
					width: 454rpx;
					height: 80rpx;
					color: #272832;

					font-weight: 100;
					font-size: 28rpx;

					.name {
						font-weight: 400;
						font-size: 36rpx;
						letter-spacing: -0.62rpx;
					}

				}

				.button {
					width: 120rpx;
					line-height: 48rpx;
					height: 48rpx;
					text-align: center;
					border-radius: 24rpx;
					font-size: 24rpx;
					letter-spacing: -0.41rpx;
					font-weight: 400;
				}

				.added {
					background: #FFE431;
					color: #272832;


				}

				.add {
					background: rgba(74, 170, 255, 0.10);
					color: #4AAAFF;
				}
			}
		}
	}
</style>
